package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import dao.CommentDAO;
import po.Comment;

/**
* @author 曹桢瑞
* CommentDAO实现
*/
public class CommentDAOImpl implements CommentDAO {
	
	private Connection con=null;
	public CommentDAOImpl(Connection con) {
		this.con=con;
	}
	@Override
	public ArrayList<Comment> getCommentByUserId(String userId, int num) {
		String sql="select * from darkcirclenovel.userrating where userId='"+userId+"' order by timeStamp desc limit "+num;
		ArrayList<Comment> list=new ArrayList<Comment>();
		try {
			PreparedStatement pstmt=con.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				String bookId=rs.getString("bookId");
				int score=rs.getInt("score");
				String content=rs.getString("comment");
				long submitTime=rs.getLong("timeStamp");
				Comment comment=new Comment(userId,bookId,score,content,submitTime);
				list.add(comment);
			}
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}
	
	@Override
	public ArrayList<Comment> getCommentByBookId(String bookId, int num) {
		String sql="select * from darkcirclenovel.userrating where bookId='"+bookId+"' order by timeStamp desc limit "+num;
		ArrayList<Comment> list=new ArrayList<Comment>();
		try {
			PreparedStatement pstmt=con.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				String userId=rs.getString("userId");
				int score=rs.getInt("score");
				String content=rs.getString("comment");
				long submitTime=rs.getLong("timeStamp");
				Comment comment=new Comment(userId,bookId,score,content,submitTime);
				list.add(comment);
			}
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}
	
	@Override
	public ArrayList<Comment> getCommentByIds(String userId, String bookId) {
		String sql="select * from darkcirclenovel.userrating where userId='"+userId+"' and bookId='"+bookId+"'";
		ArrayList<Comment> list=new ArrayList<Comment>();
		try {
			PreparedStatement pstmt=con.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				int score=rs.getInt("score");
				String content=rs.getString("comment");
				long submitTime=rs.getLong("timeStamp");
				Comment comment=new Comment(userId,bookId,score,content,submitTime);
				list.add(comment);
			}
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}
	
	@Override
	public boolean addComment(Comment comment) {
		int num = 0;
	    String sql = "insert into darkcirclenovel.userrating (userId,bookId,score,content,timeStamp) values(?,?,?,?,?,?)";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        pstmt.setString(1, comment.getUserId());
	        pstmt.setString(2, comment.getBookId());
	        pstmt.setInt(3, comment.getScore());
	        pstmt.setString(4, comment.getContent());
	        pstmt.setLong(5, comment.getSubmitTime());
	        num = pstmt.executeUpdate();
	        pstmt.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    if(num==1) {
	    	return true;
	    }
	    else {
	    	return false;
	    }
	}
	
	@Override
	public boolean updateComment(Comment comment) {
		int num = 0;
	    String sql="update darkcirclenovel.userrating set score="+comment.getScore()+",content='"+comment.getContent()+"',timeStamp="+comment.getSubmitTime()+" where userId='"+comment.getUserId()+"' and bookId='"+comment.getBookId()+"'";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        num = pstmt.executeUpdate();
	        pstmt.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    if(num==1) {
	    	return true;
	    }
	    else {
	    	return false;
	    }
	}
	
	@Override
	public boolean removeCommentByIds(String userId, String bookId) {
		int num = 0;
	    String sql = "delete from darkcirclenovel.userrating where userId='"+userId+"' and bookId='"+bookId+"'";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        num = pstmt.executeUpdate();
	        pstmt.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    if(num==1) {
	    	return true;
	    }
	    else {
	    	return false;
	    }
	}
}
